package com.jctrip.hotel.route.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jctrip.hotel.common.util.BeanUtil;
import com.jctrip.hotel.domain.dao.HtHotelMthdomInfoDao;
import com.jctrip.hotel.domain.dao.HtRoomTypeStdInfoDao;
import com.jctrip.hotel.domain.dao.HtRoomtypeMthdomInfoDao;
import com.jctrip.hotel.domain.entity.*;
import com.jctrip.hotel.route.model.param.GetHotelRoomTypeParam;
import com.jctrip.hotel.route.model.result.GetHotelRoomTypeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 获取酒店房型接口-服务
 */
@Service
public class GetHotelRoomTypeService extends RouteService {

    @Autowired
    private HtRoomTypeStdInfoDao htRoomTypeStdInfoDao;
    @Autowired
    private HtRoomtypeMthdomInfoDao htRoomtypeMthdomInfoDao;

    public GetHotelRoomTypeResult execute(GetHotelRoomTypeParam param) {
        // 价格政策配置
        List<HtPricePolicy> htPricePolicies = getPricePolicies();
        List<String> sourceCodes = htPricePolicies.stream()
                .map(HtPricePolicy::getSourceCode)
                .collect(Collectors.toList());
        // 酒店标准信息
        HtHotelStdInfo htHotelStdInfo = getHotelStdInfo(param.getHotelId(), sourceCodes);
        HtHotelMthdomInfo htHotelMthdomInfo = null;
        if(sourceCodes.contains("MTHDOM")){
            htHotelMthdomInfo = getHotelMthdomInfo(param.getHotelId());
        }
        if (htHotelStdInfo == null && htHotelMthdomInfo == null) {
            throw new RuntimeException("无效的hotelId");
        }
        // 房型标准信息
        if(param.getHotelId().contains("MTHDOM")){
            List<HtRoomtypeMthdomInfo> htRoomtypeMthdomInfoList = getMthdomRoomInfoList(param.getHotelId());
            List<GetHotelRoomTypeResult.RoomType> roomTypeDataList = htRoomtypeMthdomInfoList.stream()
                    .map(e->{
                        GetHotelRoomTypeResult.RoomType roomType = GetHotelRoomTypeResult.RoomType.builder().build();
                        BeanUtil.copyProperties(e,roomType);
                        return roomType;
                    }).collect(Collectors.toList());
            return GetHotelRoomTypeResult.builder()
                    .hotelId(param.getHotelId())
                    .roomTypeList(roomTypeDataList)
                    .build();
        }
        List<HtRoomTypeStdInfo> htRoomTypeStdInfoList = getRoomTypeStdInfoList(param.getHotelId());
        List<GetHotelRoomTypeResult.RoomType> roomTypeDataList = htRoomTypeStdInfoList.stream()
                .map(e -> {
                    GetHotelRoomTypeResult.RoomType roomType = GetHotelRoomTypeResult.RoomType.builder().build();
                    BeanUtil.copyProperties(e, roomType);
                    return roomType;
                }).collect(Collectors.toList());
        return GetHotelRoomTypeResult.builder()
                .hotelId(param.getHotelId())
                .roomTypeList(roomTypeDataList)
                .build();
    }

    private List<HtRoomTypeStdInfo> getRoomTypeStdInfoList(String hotelId) {
        LambdaQueryWrapper<HtRoomTypeStdInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HtRoomTypeStdInfo::getHotelId, hotelId);
        return htRoomTypeStdInfoDao.selectList(queryWrapper);
    }
    private List<HtRoomtypeMthdomInfo> getMthdomRoomInfoList(String hotelId) {
        LambdaQueryWrapper<HtRoomtypeMthdomInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HtRoomtypeMthdomInfo::getHotelId,hotelId);
        return htRoomtypeMthdomInfoDao.selectList(queryWrapper);
    }
}
